<template>
	<view class="login">
		<view class="message_top">
			<view class="message_top_title" :style="{top:computedTop}">登录</view>
			<view class="message_top_back" :style="{top:computedTop}" @click="Back">
				<image src="../../static/search/back.png" class="bfbimages"></image>
			</view>
		</view>

		<view class="login_logo flex">
			<image src="../../static/loginLogo.png" class="bfbimages"></image>
		</view>

		<!-- <view class="login_btn" @click="Clogin">
			<view class="flex">
				<image src="../../static/weixin.png" class="bfbimages"></image>
			</view>
			<text>一键登录</text>
		</view> -->

		<view class="login_btn">
			<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号授权登录</button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				menuButtonInfo: null,
				clickTimer: null,
				OCDE: ''
			}
		},
		computed: {
			computedTop() {
				if (this.menuButtonInfo) {
					return `${this.menuButtonInfo.height * 2 - 6}px`;
				}
				return '0px';
			},
		},
		onShow() {
			uni.login({
				success: (res) => {
					this.OCDE = res.code
				}
			})
		},
		onLoad() {
			const info = uni.getMenuButtonBoundingClientRect();
			this.menuButtonInfo = info;
		},
		methods: {
			getPhoneNumber(e) {
				if (e.detail.errMsg !== 'getPhoneNumber:ok') return uni.$showMsg('授权失败')
				if (e.detail.code !== undefined) {
					uni.showLoading({
						title: '正在登录'
					});
					this.datas = {
						code: this.OCDE,
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
					}
					this.ClickLogin()
				} else {
					console.log('拒绝了');
				}
			},
			ClickLogin() {
				uni.request({
					url: this.$uts + '/api/login/wxLogin',
					method: 'POST',
					data: this.datas,
					header:{
						'server':1
					},
					success: res => {
						console.log(res, '微信登录');
						if (res.data.code == 1) {
							uni.hideLoading();
							uni.setStorageSync('token', res.data.data.user_token)
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 1500
							});
							setTimeout(() => {
								uni.navigateBack()
							}, 1500)
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							return false
						}
					},
				})
			},
			Back() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	.login {
		width: 100vw;
		height: 100vh;
		float: left;

		.message_top {
			width: 100%;
			height: 200rpx;
			position: relative;

			.message_top_title {
				width: 100%;
				text-align: center;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 36rpx;
				color: #333333;
				position: absolute;
				left: 0;
				z-index: 2;
			}

			.message_top_back {
				width: 44rpx;
				height: 44rpx;
				position: absolute;
				left: 30rpx;
				z-index: 9;
			}
		}

		.login_logo {
			width: 416rpx;
			height: 65rpx;
			margin: 250rpx auto 0;
		}

		.login_btn {
			width: 615rpx;
			height: 99rpx;
			background: linear-gradient(270deg, #FE4624 0%, #FF8143 100%);
			border-radius: 60rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 30rpx;
			color: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 130rpx auto 0;
		}

		.login_btn>view {
			width: 30rpx;
			height: 30rpx;
			margin-right: 15rpx;
		}
	}

	button {
		all: unset;
	}
</style>